#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import re

from django.contrib.auth.backends import ModelBackend

from users.models import User


def get_user_by_account(account):
    """
    通过传入的账号动态获取user 模型对象
    :param account:  有可以是手机号,有可能是用户名
    :return:  user或None
    """
    try:
        if re.match(r'1[3-9]\d{9}$', account):
            user = User.objects.get(mobile=account)
        else:
            user = User.objects.get(username=account)
    except User.DoesNotExist:
        return None  # 如果没有查到返回None
    else:
        return user  # 注意不要写在模型类


class UsernameMobileAuthBackend(ModelBackend):
    """修改Django的认证类,为了实现多账号登录"""

    def authenticate(self, request, username=None, password=None, **kwargs):
        """"
        获取到user，判断当前传入的密码是否正确
        """
        user = get_user_by_account(username)
        # 判断当前前端传入的密码是否正确
        if user and user.check_password(password):
            # 返回user
            return user
